package cn.exrick.controller;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.exrick.common.exception.XmallException;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.common.SMSCode;
import cn.exrick.utils.ResultUtil;
import cn.exrick.utils.SMSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(description = "短信验证码接口")
public class SMSController {

	@Value("${sms.temp}")
	public String temp;
	@Autowired
	SMSUtil smsUtil;

	public static final long time = 60 * 1000;// 1分钟间隔
	public static final long sameTime = 6 * 60 * 1000;// 6分钟内发送的验证码一样
	public static final long exTime = 30 * 60 * 1000;// 30分钟过期

	

	public static ConcurrentHashMap<String, SMSCode> map = new ConcurrentHashMap<>();

	public static boolean isPhone(String phone) {
		String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
		if (phone.length() != 11) {
			return false;
		} else {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(phone);
			boolean isMatch = m.matches();
			return isMatch;
		}
	}

	/**
	 * 生成随机验证码
	 *
	 * @return
	 */
	public static String generateRegisterCode(String phone) {
		SMSCode smsCode = map.get(phone);
		if (smsCode == null) {
			int code = 1 + (int) (Math.random() * ((999999 - 1) + 1));
			smsCode = new SMSCode(phone, String.valueOf(code), new Date(), new Date());
			map.put(phone, smsCode);
			return smsCode.getCode();
		} else {
			if (new Date().getTime() - smsCode.getDate().getTime() < time) {
				throw new XmallException("获取短信验证码时间间隔不得低于" + time / 1000 + "秒");
			} else if (new Date().getTime() - smsCode.getGenerate().getTime() < sameTime) {
				smsCode.setDate(new Date());
				map.put(phone, smsCode);
				return smsCode.getCode();
			} else {
				int code = 1 + (int) (Math.random() * ((999999 - 1) + 1));
				smsCode = new SMSCode(phone, String.valueOf(code), new Date(), new Date());
				map.put(phone, smsCode);
				return smsCode.getCode();
			}
		}
	}

	/**
	 * 校验验证码是否正确
	 *
	 * @param phone
	 * @param code
	 * @return
	 */
	public static boolean checkCode(String phone, String code) {
		SMSCode smsCode = map.get(phone);
		if (smsCode != null && (new Date().getTime() - smsCode.getDate().getTime() < exTime)
				&& smsCode.getCode().equals(code)) {
			return true;
		}
		return false;
	}

	@RequestMapping(value = "/sms/code", method = RequestMethod.GET)
	@ApiOperation(value = "发送验证码")
	public Result<Boolean> register(String phone) {
		if (!isPhone(phone)) {
			throw new XmallException("手机号不合法");
		}
		String code = generateRegisterCode(phone);
		String content = temp;
		content = content.replaceAll("\\{code}", code).replaceAll("\\{exTime}", String.valueOf(exTime / 60 / 1000));
		boolean result = smsUtil.sendSms(phone, content);
//        boolean result = true;
		if (!result) {
			throw new XmallException("短信验证码发送失败,请稍后重试");
		}
		return new ResultUtil<Boolean>().setData(null);
	}
}
